//
//  PARangeListVC.m
//  PilotSeries
//
//  Created by 黄立强 on 16/7/1.
//  Copyright © 2016年 leon.huang. All rights reserved.
//

#import "PARangeListVC.h"
#import "PAAppContext.h"
#import "PARangeListCell.h"
#import "RangeListData.h"
#import "PAAccountManager.h"
#import "PAChartConfigManager.h"
#import "PACreateChartConfigVC.h"

@interface PARangeListVC ()

@property (nonatomic, strong) NSArray * datas;
@end

@implementation PARangeListVC

#pragma mark - 懒加载
- (NSArray *)datas
{
    if (_datas == nil) {
        _datas = [PAAppContext sharedInstance].rangelists;
    }
    return _datas;
}
static NSString * const PARangeListCell_ID = @"rangelist_cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置tableView属性
    self.tableView.backgroundColor = PAGlobalBg;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.title = @"ServerRanges";
    
    UIBarButtonItem *createChart  =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(createDirection:)];
    // 设置导航栏左边的按钮
    self.navigationItem.rightBarButtonItems =@[createChart];
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PARangeListCell class]) bundle:nil] forCellReuseIdentifier:PARangeListCell_ID];
}

///** 屏蔽tableView的样式 */
//- (id)init
//{
//    return [self initWithStyle:UITableViewStyleGrouped];
//}
#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.datas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PARangeListCell *cell = [tableView dequeueReusableCellWithIdentifier:PARangeListCell_ID];
    RangeListData *range = self.datas[indexPath.row];
    cell.textLabel.text =range.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (![self checkDockSize]) return;
    RangeListData *rangeData = self.datas[indexPath.row];
    PACreateChartConfigVC *ccvc = [PACreateChartConfigVC new];
    ccvc.rangeData = rangeData;
    [self.navigationController pushViewController:ccvc animated:YES];
    
}

- (BOOL)checkDockSize
{
    NSInteger size = [PAAccountManager loadCurrentDockSize];
    NSArray *chartConfigs =  [PAChartConfigManager loadCurrentChartConfigs];
    if (size<=chartConfigs.count) {
        [CRToastManager showNotificationWithMessage:@"already more than limit chart size!please change dockSize first!" completionBlock:^{
            
        }];
        return NO;
    }
    return YES;
}

-(void)createDirection:(UIBarButtonItem *)buttonItem {
    NSLog(@"createDirection");
}

@end
